package com.zgkxzx.java.net;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 创建者   zgkxzx
 * 创建日期 2018/1/27.
 * 功能描述
 */

public class VaildUtils {

    private static final String TAG = "VaildUtils";

    private String xAppId = "15dUA5mvxvGyurW47r8Wqs4X-gzGzoHsz";
    private String xAppKey = "pd2BzHtpE1qCKf4IAwsHfQYR";
    private String url = "https://leancloud.cn:443/1.1/classes/ControlInfo";

    public VaildUtils(String url, String xAppId, String xAppKey) {
        this.url = url;
        this.xAppId = xAppId;
        this.xAppKey = xAppKey;
    }


    public synchronized void isClose(final OnResultBack onResultBack) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String str = get(url, xAppId, xAppKey);
                Log.e(TAG, "网络接受的数据 " + str);
                if (str == null) {
                    if (onResultBack != null)
                        onResultBack.onResult(false);
                    return;
                } else if (str.equals("")) {
                    if (onResultBack != null)
                        onResultBack.onResult(true);
                    return;
                }

                try {
                    JSONObject jsonObject = new JSONObject(str);
                    JSONArray results = (JSONArray) jsonObject.get("results");
                    JSONObject result = (JSONObject) results.get(0);
                    boolean isClose = (boolean) result.get("isClose");

                    Log.e(TAG, "results= " + results + " isClose=" + isClose);

                    if (onResultBack != null)
                        onResultBack.onResult(isClose);
                    return;
                } catch (Exception e) {
                    e.printStackTrace();
                    if (onResultBack != null)
                        onResultBack.onResult(true);
                    return;
                }
            }
        }).start();
    }


    public interface OnResultBack {
        void onResult(boolean isVaild);
    }

    private String get(String urlStr, String appId, String appKey) {
        HttpURLConnection httpURLConnection = null;
        try {

            URL url = new URL(urlStr);

            httpURLConnection = (HttpURLConnection) url.openConnection();

            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setReadTimeout(5000);
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setRequestProperty("X-LC-Id", appId);
            httpURLConnection.setRequestProperty("X-LC-Key", appKey);

            int responseCode = httpURLConnection.getResponseCode();
            if (responseCode == 200) {
                InputStream inputStream = httpURLConnection.getInputStream();
                String stringFromInputStream = getStringFromInputStream(inputStream);
                return stringFromInputStream;
            } else {
                return "";
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (httpURLConnection != null)
                httpURLConnection.disconnect();
        }

        return null;
    }

    private String getStringFromInputStream(InputStream is)
            throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        // 模板代码 必须熟练
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = is.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
        is.close();
        String state = os.toString();// 把流中的数据转换成字符串,采用的编码是utf-8(模拟器默认编码)
        os.close();
        return state;
    }
}
